package com.sskj.quantization.ui.fragment;


import android.support.design.widget.BottomSheetDialog;
import android.support.v7.widget.AppCompatTextView;
import android.widget.Button;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.shizhefei.view.coolrefreshview.CoolRefreshView;
import com.shizhefei.view.coolrefreshview.SimpleOnPullListener;
import com.sskj.common.util.ClickUtil;
import com.sskj.lib.RConfig;
import com.sskj.lib.base.BaseFragment;
import com.sskj.lib.bean.UserData;
import com.sskj.lib.model.room.UserViewModel;
import com.sskj.lib.util.AppCircleCheckUtil;
import com.sskj.lib.util.AppDirectCheckUtil;
import com.sskj.lib.util.AppOuterCheckUtil;
import com.sskj.lib.util.CoinUtil;
import com.sskj.quantization.R;
import com.sskj.quantization.R2;
import com.sskj.quantization.bean.QuanBean;
import com.sskj.quantization.component.DaggerUserDataComponent;
import com.sskj.quantization.presenter.QuantizationFragmentPresenter;
import com.sskj.quantization.util.TipQuanUtil;

import javax.inject.Inject;

import butterknife.BindView;

@Route(path = RConfig.QUAN_FRAGMENT_MAIN)
public class QuantizationFragment extends BaseFragment<QuantizationFragmentPresenter> {
    @BindView(R2.id.btBuy)
    Button btBuy;
    @BindView(R2.id.tvRecord)
    AppCompatTextView tvRecord;
    @BindView(R2.id.tvProfit)
    AppCompatTextView tvProfit;
    @BindView(R2.id.tvHoldUSDT)
    TextView tvHoldUSDT;
    @BindView(R2.id.tvHoldNum)
    TextView tvHoldNum;
    @BindView(R2.id.tvHoldUSDTPre)
    TextView tvHoldUSDTPre;
    @BindView(R2.id.tvHoldUSDAccumulate)
    TextView tvHoldUSDAccumulate;
    @BindView(R2.id.tvHoldUSDTPreRep)
    TextView tvHoldUSDTPreRep;
    @BindView(R2.id.tvHoldUSDAccumulateRep)
    TextView tvHoldUSDAccumulateRep;
    @BindView(R2.id.tvUSDT)
    AppCompatTextView tvUSDT;
    @BindView(R2.id.tvRate)
    AppCompatTextView tvRate;
    @BindView(R2.id.tvInvite)
    TextView tvInvite;
    @BindView(R2.id.coolRefreshView)
    CoolRefreshView coolRefreshView;
    private QuanBean quanBean;
    private BottomSheetDialog buyTip;
    @Inject
    UserViewModel userViewModel;
    private UserData userData;


    @Override
    protected int getLayoutId() {
        return R.layout.quan_fragment_main;
    }

    @Override
    public QuantizationFragmentPresenter getPresenter() {
        return new QuantizationFragmentPresenter();
    }

    @Override
    protected void initView() {
        DaggerUserDataComponent.create().inject(this);
        userViewModel.getUsers().observe(this, users -> {
            if (users != null && users.size() > 0) {
                userData = users.get(0);
                mPresenter.getQuanData();
            } else {
                userData = null;
                updateUI(null);
            }

        });
        ClickUtil.click(btBuy, () -> {
            if (!AppDirectCheckUtil.checkLogin(getActivity(), userData)) {
                return;
            }
            if (!AppCircleCheckUtil.checkTradePwd(getActivity(), userData)) {
                return;
            }
            buyTip = TipQuanUtil.showBuy(getActivity(), quanBean.getCon_money(), quanBean.getCon_money_db(), input -> {
                buyTip.dismiss();
                mPresenter.buy(input[0]);
            });
            buyTip.show();
        });
        ClickUtil.click(tvRecord, () -> {
            if (!AppDirectCheckUtil.checkLogin(getActivity(), userData)) {
                return;
            }
            ARouter.getInstance().build(RConfig.QUAN_RECORD).navigation();
        });
        ClickUtil.click(tvProfit, () -> {
            if (!AppDirectCheckUtil.checkLogin(getActivity(), userData)) {
                return;
            }
            ARouter.getInstance().build(RConfig.QUAN_PROFIT).navigation();
        });
        coolRefreshView.addOnPullListener(new SimpleOnPullListener() {
            @Override
            public void onRefreshing(CoolRefreshView refreshView) {
                if (userData == null)
                    return;
                mPresenter.getQuanData();
            }
        });
        ClickUtil.click(tvInvite, () -> {
            ARouter.getInstance().build(RConfig.MINE_INVITE).navigation();
        });

    }

    @Override
    protected void initData() {

    }

    public void updateUI(QuanBean data) {
        if (coolRefreshView != null && coolRefreshView.isRefreshing()) {
            coolRefreshView.setRefreshing(false);
        }
        if (tvHoldUSDT == null)
            return;
        if (data == null) {
            tvHoldUSDT.setText(CoinUtil.keepCoin("0"));
            tvHoldNum.setText("0");
            tvHoldUSDTPre.setText(String.format("%s USD", CoinUtil.keepUSDT("0")));
            tvHoldUSDTPreRep.setText(String.format("≈%s GRCT", CoinUtil.keepCoin("0")));
            tvHoldUSDAccumulate.setText(String.format("%s USD", CoinUtil.keepUSDT("0")));
            tvHoldUSDAccumulateRep.setText(String.format("≈%s GRCT", CoinUtil.keepCoin("0")));
            tvUSDT.setText(String.format("%s USD", CoinUtil.keepUSDT("0")));
            tvRate.setText(String.format("0%%"));
        } else {
            this.quanBean = data;
            tvHoldUSDT.setText(CoinUtil.keepCoin(data.getMoney()));
            tvHoldNum.setText(data.getCount());
            tvHoldUSDTPre.setText(String.format("%s USD", CoinUtil.keepUSDT(data.getYg_profit())));
            tvHoldUSDTPreRep.setText(String.format("≈%s GRCT", CoinUtil.keepCoin(data.getYg_profit_db())));
            tvHoldUSDAccumulate.setText(String.format("%s USD", CoinUtil.keepUSDT(data.getProfit())));
            tvHoldUSDAccumulateRep.setText(String.format("≈%s GRCT", CoinUtil.keepCoin(data.getProfit_db())));
            tvUSDT.setText(String.format("%s USD", CoinUtil.keepUSDT(data.getCon_money())));
            tvRate.setText(String.format("%s%%", data.getCon_rate()));
        }


    }

    public void refreshFinish() {
        if (coolRefreshView != null && coolRefreshView.isRefreshing()) {
            coolRefreshView.setRefreshing(false);
        }
    }

    public void buySuccess() {
        if (coolRefreshView != null && !coolRefreshView.isRefreshing()) {
            coolRefreshView.setRefreshing(true);
        }
    }
}
